/*
 * @Author: lhl 1079161148@qq.com
 * @Date: 2024-04-04 18:04:26
 * @LastEditors: lhl 1079161148@qq.com
 * @LastEditTime: 2024-04-04 18:29:15
 * @FilePath: \net-cloud-music\src\utils\secret.ts
 * @Description: AES 加密 解密
 * import { Encrypt, Decrypt } from "@/utils/secret.ts";
 * 加密：
 * Encrypt(token)
 * 解密：Decrypt(token)
 */
import CryptoJS from 'crypto-js'

// 必须为16、24、32位
// 十六位十六进制数作为秘钥
// 十六位十六进制数作为秘钥偏移量
const KEY = '0123456789abcdef'
const IV = '1234567812345678'

/**
 * AES加密 ：字符串 key iv  返回base64
 */
export const Encrypt = (word: any, keyStr = KEY, ivStr = IV): any => {
  const key = CryptoJS.enc.Utf8.parse(keyStr)
  const iv = CryptoJS.enc.Utf8.parse(ivStr)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  })
  return encrypted.toString()
}

/**
 * AES 解密 ：字符串 key iv  返回base64
 *
 * @return {string}
 */
export const Decrypt = (word: any, keyStr = KEY, ivStr = IV): any => {
  const key = CryptoJS.enc.Utf8.parse(keyStr)
  const iv = CryptoJS.enc.Utf8.parse(ivStr)
  const decrypt = CryptoJS.AES.decrypt(word, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  })
  return CryptoJS.enc.Utf8.stringify(decrypt)
}
